package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.file.MinIOTemplate;
import com.heima.file.config.MinIoAutoConfiguration;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.Util.WmThreadLocalUtil;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.model.wemedia.entity.WmUser;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 16:22:33
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    MinIoAutoConfiguration minIoAutoConfiguration;

    @Resource
    private WmMaterialDao wmMaterialDao;

    @Autowired
    MinIOTemplate minIOTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    //上传素材
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //上传图片到minio
        String originalFilename = multipartFile.getOriginalFilename();
        //获取到.开始的后缀
        String s = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //得到一个新的图片名
        String filenName = uuid + s;
        String url = minIOTemplate.uploadImgFile("", filenName, multipartFile.getInputStream());

        //获取线程userId
        WmUser wmUser = WmThreadLocalUtil.getUser();
        WmMaterial wmMaterial = new WmMaterial();
        //把图片信息放到对应的表中
        wmMaterial.setUserId(Long.valueOf(wmUser.getId()));
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection(0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());
        wmMaterialDao.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    //删除图片
    @Override
    public ResponseResult deleteById(Long id) {
        if (id == null || id == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmMaterialDao.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //更改为收藏
    @Override
    public ResponseResult updateById(Integer id) {
        if (id == null || id == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmMaterial wmMaterial = wmMaterialDao.queryById(Long.valueOf(id));
        wmMaterial.setId(Long.valueOf(id));
        wmMaterial.setIsCollection(1);
        int update = wmMaterialDao.update(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //取消收藏
    @Override
    public ResponseResult updatesByIds(Integer id) {
        if (id == null || id == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmMaterial wmMaterial = wmMaterialDao.queryById(Long.valueOf(id));
        wmMaterial.setIsCollection(0);
        int update = wmMaterialDao.update(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //分页查询
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //判断数据是否为空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        dto.checkParam();
        //执行分页查询条件
        WmMaterial wmMaterial = new WmMaterial();
        Integer id = WmThreadLocalUtil.getUser().getId();
        wmMaterial.setUserId(Long.valueOf(id));
        //看分页查询的是否为收藏分页查询
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            wmMaterial.setIsCollection(dto.getIsCollection());
        }
        //进行分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmMaterial> page = wmMaterialDao.queryAllByLimit(wmMaterial);

        //将数据封装返回
        ResponseResult result = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }
}
